package com.haierp.service.authority.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.authority.RoleMapperHai;
import com.haierp.mapper.authority.RoleResourceMapperHai;
import com.haierp.mapper.authority.UserRoleMapperHai;
import com.haierp.model.authority.RoleHai;
import com.haierp.model.authority.RoleResourceHai;
import com.haierp.service.authority.IRoleServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.authority.RoleQueryVO;

/**
 * 
 * Title: RoleServiceImpl.java
 * Description: 
 *
 * @author jc
 * Apr 23, 2017
 *
 */
@Service("roleServiceHai")
public class RoleServiceHaiImpl extends SuperServiceImpl<RoleMapperHai, RoleHai> implements IRoleServiceHai {

    @Autowired
    private RoleMapperHai roleMapperHai;
    
    @Autowired
    private RoleResourceMapperHai roleResourceMapperHai;
    
    @Autowired
    private UserRoleMapperHai userRoleMapperHai;
    
    @Override
    public Integer queryRolesCountByName(String name, Long id)
    {
    	RoleHai role = new RoleHai();
    	role.setName(name);
    	
    	EntityWrapper<RoleHai> wrapper = new EntityWrapper<RoleHai>(role);
    	if (null != id) {
            wrapper.where("id != {0}", id);
        }
    	return this.selectCount(wrapper);
    }
    
    @Override
    public Integer queryRolesCount(RoleQueryVO roleQueryVO) {
		return roleMapperHai.queryRolesCount(roleQueryVO);
	}

	@Override
	public void deleteRoleById(Long id)
	{
		roleMapperHai.deleteById(id);
		
		List<Long> list = roleResourceMapperHai.queryIdListByRoleId(id);
		if (list != null && !list.isEmpty()) {
	        for (Long roleResourceId : list) {
	        	roleResourceMapperHai.deleteById(roleResourceId);
	        }
	    }
	}
    
	@Override
	public JsonPageResult<List<RoleHai>> queryRoleList(RoleQueryVO roleQueryVO) {
		JsonPageResult<List<RoleHai>> result = new JsonPageResult<>();
		
		Integer totalCount = roleMapperHai.queryRolesCount(roleQueryVO);
		
		if ((null != totalCount) && (0L != totalCount)) {
			result.buildPage(totalCount, roleQueryVO);
			
			List<RoleHai> roles = roleMapperHai.queryRoleQueryList(roleQueryVO);
			result.setData(roles);			
		} else {
			List<RoleHai> roles = new ArrayList<>();
			result.setData(roles);
		}
		
		return result;
	}

    @Override
    public List<Long> queryResourceIdListByRoleId(Long id) {
        return roleMapperHai.queryResourceIdListByRoleId(id);
    }
    
    @Override
    public Map<String, Set<String>> queryResourceMapByUserId(Long userId) {
        Map<String, Set<String>> resourceMap = new HashMap<String, Set<String>>();
        List<Long> roleIdList = userRoleMapperHai.queryRoleIdListByUserId(userId);
        Set<String> urlSet = new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        for (Long roleId : roleIdList) {
            List<Map<Long, String>> resourceList = roleMapperHai.queryResourceListByRoleId(roleId);
            if (resourceList != null) {
                for (Map<Long, String> map : resourceList) {
                    if (StringUtils.isNotBlank(map.get("url"))) {
                        urlSet.add(map.get("url"));
                    }
                }
            }
            RoleHai role = roleMapperHai.selectById(roleId);
            if (role != null) {
                roles.add(role.getName());
            }
        }
        resourceMap.put("urls", urlSet);
        resourceMap.put("roles", roles);
        return resourceMap;
    }

    @Override
    public void updateRoleResource(Long roleId, String resourceIds) {
        // 先删除后添加,有点爆力
        RoleResourceHai roleResource = new RoleResourceHai();
        roleResource.setRoleId(roleId);
        roleResourceMapperHai.deleteSelective(roleResource);
        
        String[] resourceIdArray = resourceIds.split(",");
        for (String resourceId : resourceIdArray) {
            roleResource = new RoleResourceHai();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(Long.parseLong(resourceId));
            roleResourceMapperHai.insert(roleResource);
        }
    }
}